<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/6
 * Time: 上午11:29
 *
 */

namespace app\apitemp\controller;


use Alipay\Alipayobj;
use app\common\model\Agent;
use app\common\model\CardGeneral;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\Tklog;
use app\common\model\WxOpen;
use Wechat\Wxobjopen;
use Weixinpay\Wxtk;

class Order extends Pub
{


    public function create_order($param)
    {
        $out_trade_no = $this->getorderid($param['merchantid']);
        $data['mid'] = $param['merchantid'];
        $data['orderid'] = $out_trade_no;
        $data['source'] = $param['source'];
        $data['pay_fs'] = $param['channel'];
        $data['d_cashier'] = $param['device_id'];
        $data['kuantai'] = $param['kuantai'];
        $data['price'] = $param['total_fee'];
        $data['yf_price'] = $param['total_fee'];
        $data['pay_cate'] = $param['pay_cate'];
        $data['add_time'] = time();
        $Paylog = new Paylog();
        $add = $Paylog->save($data);
        $infos = ['out_trade_no' => $out_trade_no];
        if ($add) {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = '';
            $rarr['info'] = $infos;
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '订单号获取失败,请尝试重新发起支付';
        }

        return $rarr;
    }

    //订单详情
    public function orderinfo($param)
    {
        try {
//            $check = $this->checkcashier($param);    //核实收银员
//            if ($check == 0) {
            $Paylog = new Paylog();
            $map['orderid'] = $param['out_trade_no'];
            $map['mid'] = $param['merchantid'];
            $info = $Paylog->where($map)->find();
            if ($info) {
                $infos['trade_no'] = $info['trade_no'];
                $infos['out_trade_no'] = $info['orderid'];
                $infos['total_fee'] = $info['price'];//实付金额
                $infos['channel'] = $info['pay_fs'];//实付金额
                $infos['ought_fee'] = $info['yf_price'];//应付金额
                $infos['gel_fee'] = $info['zk_price'];//折扣金额
                $infos['dis_card_zk'] = $info['dis_card_zk'];        //会员卡折扣     field('dis_card_zk,vip_card_zk,gel_card_zk,hx_card')->
                $infos['vip_card_zk'] = $info['vip_card_zk'];        //等级折扣
                $infos['gel_card_zk'] = $info['gel_card_zk'];        //卡券抵扣
                $infos['service_charge'] = $info['service_charge'];        //卡券抵扣
                $infos['end_time'] = !empty($info['pay_time']) ? $info['pay_time'] : "";//支付时间
                $infos['gel_info'] = getdeduzkinfo($info['hx_card'], $info['gel_card_zk']);        //抵扣明细
                $infos['is_vip'] = "";        //是否会员
                $infos['is_fans'] = "";        //是否会员
                $infos['card_name'] = "";        //会员姓名
                $infos['card_num'] = $info['card_num'];        //会员卡号
                $infos['refund_fee'] = $info['tk_price'];        //退款金额
                $Tklog = new Tklog();
                $tklist = [];
                $tkinfo = $Tklog->where(array('pid' => $info['id'], 'tk_status' => 1))->field('tk_orderid,tk_fs,action_user,refund_fee,addtime')->select();
                foreach ($tkinfo as $k => $v) {
                    $tklist[$k]['refund_orderid'] = $v['tk_orderid'];
                    $tklist[$k]['refund_source'] = $v['tk_fs'];
                    $tklist[$k]['refund_user'] = $v['action_user'];
                    $tklist[$k]['refund_fee'] = $v['refund_fee'];
                    $tklist[$k]['refund_time'] = $v['addtime'];
                }
                $infos['refund_list'] = $tklist;
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '';
                $rarr['info'] = $infos;
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '订单查询失败';
            }
//            } else {
//                $rarr['return_code'] = "SUCCESS";
//                $rarr['return_msg'] = '';
//                $rarr['result_code'] = 'FAIL';
//                $rarr['result_msg'] = '您无权限查询该订单';
//            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    public function getorderprice($param)
    {
        try {
            /*
             *
             *
             * 此处未修复
             *
             *
             * */
//            $map['mid'] = $param['merchantid'];
//            $User = new Merchant();
//            $shtd = $User->where('id', $param['merchantid'])->value('shtd');
//            if ($shtd == 0) {
//                $map['orderid'] = $param['out_trade_no'];
//            } elseif ($shtd == 1) {
                $map['ms_orderid|trade_no|orderid'] = $param['out_trade_no'];
//            } elseif ($shtd == 10) {
//                $map['trade_no'] = $param['out_trade_no'];
//            } else {
//                $map['orderid'] = $param['out_trade_no'];
//            }
            $Paylog = new Paylog();
            $order = $Paylog->where($map)->find();
//        $addtime=;
            $Tklog = new Tklog();
            $map2['tk_status'] = 1;
            $map2['orderid'] = $order['orderid'];
            $ytk = $Tklog->where($map2)->sum('refund_fee');
            if ($order) {
                $arr = array('out_trade_no' => $order['orderid'],
                    'channel' => $order['pay_fs'],
                    'total_fee' => $order['price'],
                    'refund_fee' => $ytk,
                    't_orderid' => $order['orderid'],
                    'orderno' => $order['orderno'],
                    'device_id' => !empty($order['cashier']) ? $order['cashier'] : $order['d_cashier'],
                    'end_time' => date('Y-m-d H:i:s', $order['add_time']),
                    'openid' => $order['openid']);
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '获取成功';
                $rarr['info'] = $arr;
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '订单编号不存在';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;


    }

    public function getorderrefund($param)
    {

        try {

//            $rarr['return_code'] = "SUCCESS";
//            $rarr['return_msg'] = '您没有退款权限';
//            $rarr['result_code'] = 'FAIL';
//            $rarr['result_msg'] = "您没有退款权限";
//            return $rarr;

            $map['orderid|trade_no'] = $param['out_trade_no'];
            $map['mid'] = $param['merchantid'];
            $Paylog = new Paylog();
            $payinfo = $Paylog->where($map)->find();
            if ($payinfo) {

                switch ($payinfo['pay_way']) {
                    case '0':

                        $OfficialOrder = new OfficialOrder();
                        $rarr = $OfficialOrder->getorderrefund($param, $payinfo['pay_fs']);
                        break;

                    case '1':
                        $rarr = $this->msrefund($param);
                        break;
                    case '2':
                        $NotCardOrder = new NotCardOrder();
                        $rarr = $NotCardOrder->getorderrefund($param, $payinfo['pay_fs']);
                        break;
                    case "10":
                        $LftOrder = new LftOrder();
                        $rarr = $LftOrder->orderrefund($param);
                        break;
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '订单编号不存在';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = "订单编号不存在";
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    public function getorderreverse($param)
    {
        try {

            $map['orderid'] = $param['out_trade_no'];
            $map['mid'] = $param['merchantid'];
            $Paylog = new Paylog();
            $payinfo = $Paylog->where($map)->field('pay_way,pay_fs')->find();
            switch ($payinfo['pay_way']) {
                case "0":
                    $OfficialOrder = new OfficialOrder();
                    $rarr = $OfficialOrder->getorderreverse($param, $payinfo['pay_fs']);
                    break;
                case "1":
                    $rarr = $this->msreverse($param);
                    break;
                case "2":
                    $NotCardOrder = new NotCardOrder();
                    $rarr = $NotCardOrder->getorderreverse($param, $payinfo['pay_fs']);
                    break;
                case "10":
                    $LftOrder = new LftOrder();
                    $rarr = $LftOrder->orderreverse($param);
                    break;
            }
//            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = "系统错误" . $e->getMessage();
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = "系统错误" . $e->getMessage();
        }
        return $rarr;
    }


}